package edu.zut.hb.bailan0.consumer;
import com.rabbitmq.client.*;
import edu.zut.hb.bailan0.producter.ConnectionUtils;

import java.io.IOException;
import java.net.Socket;
import java.util.concurrent.TimeoutException;


public class SecondConsumer extends Thread {
    private Socket clientSocket;

    public SecondConsumer(Socket clientSocket) {
        this.clientSocket = clientSocket;
    }

    @Override
    public void run() {
        //1 创建一个ConnectionFactory, 并进行配置
        //2 通过连接工厂创建连接
        //SecondConsumer
        try{
            Connection connection = ConnectionUtils.getConnection();
            Channel channel = connection.createChannel();
            boolean flag = true;
            getConsumer gc=new getConsumer();
            while (flag) {
                Consumer consumer = new DefaultConsumer(channel) {
                    @Override
                    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                        System.out.println(gc.getConsumername() + new String(body));
                        System.out.println("完成事件B");
                    }
                };
                channel.basicConsume("test_fanout_queue2", true, consumer);
            }
        }catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }

    }
}


